package com.taoyun.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.taoyun.mapper.*;
import com.taoyun.pojo.entity.Group;
import com.taoyun.pojo.entity.User;
import com.taoyun.pojo.vo.GroupMember;
import com.taoyun.pojo.vo.GroupVO;
import com.taoyun.service.IGroupService;
import com.taoyun.service.IObsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lkxicy
 * @since 2020-09-23
 */
//extends ServiceImpl<GroupMapper, Group>
@Service
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
public class GroupServiceImpl implements IGroupService {

    @Autowired
    private GroupMapper groupMapper;

    @Autowired
    private IObsService obsService;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private FileBelongGroupMapper fileBelongGroupMapper;

    @Autowired
    private USendFMapper uSendFMapper;

    @Autowired
    private FileMapper fileMapper;
    /**
     * 查询所属的群组
     * @param AdminId
     * @return GroupList
     */
    @Override
    public List<Group> ShowGroup(Integer AdminId){
        List<Group> GroupList =  groupMapper.ShowGroup(AdminId);
        if(GroupList.size()!=0 && GroupList!=null){  //判断有没有群组
            return GroupList;
        }else{ //没有返回null
            return null;
        }
    }

    /**
     * 通过GroupId查询群信息，返回所有群员信息
     * @param GroupId
     * @return List<Group> groupList
     */
    @Override
    public List<Group> ShowInformationOfGroup(Integer GroupId){
        List<Group> groupList = groupMapper.ShowInformationOfGroup(GroupId);
        if(groupList.size()!=0){
            return groupList;
        }else{
            return null;
        }
    }

    /**
     * 删除群组成员
     * @param GroupId
     * @param Ids
     * @return
     */
    public Integer DeleteUserOfGroup(Integer GroupId, List<Integer> Ids){
//        List<GroupVO> DeleteList = new ArrayList<>();
//        for(Integer uid:Ids){
//            GroupVO temp = new GroupVO();
//            temp.setUId(uid);
//            temp.setGroupId(GroupId);
//            DeleteList.add(temp);
//        }
        int DeleteResult = groupMapper.DeleteUserOfGroup(GroupId,Ids);
        if(DeleteResult == Ids.size()){
            return 1;
        }else{
            return 0;
        }
    }

    /**
     * 通过群组Id查询群组信息
     * @param GroupId
     * @return
     */
    @Override
    public Group SelectGroupByIdAndUId(Integer GroupId,Integer uId){
        Group group = groupMapper.SelectGroupByIdAndUId(GroupId,uId);
        if(group!=null){
            return group;
        }else{
            return null;
        }
    }


    /**
     * 创建群组
     * @param group
     * @return Integer
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, readOnly = false)
    public Integer CreateGroup(Group group, Integer AdminId) throws IOException {
        if(groupMapper.CreateGroup(group) == 1){
            //创建群组文件夹，并且添加ObjectSize、file表的记录
            User Admin = userMapper.SearchStaffByID(AdminId);
            Boolean result = obsService.putObject("groups/"+ group.getGroupId().toString(), Admin.getName(), "group", AdminId);
            if(result){
                return 1;
            }else {
                return -1;
            }
        }else{
            return 0;
        }
    }

    /**
     * 拉取员工进群处理
     * @param Ids
     * @param GroupId
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, readOnly = false)
    public Integer SelectUserIntoGroup(List<Integer> Ids,Integer GroupId,Integer uId){
        Group g = groupMapper.SelectGroupByIdAndUId(GroupId,uId);  //通过群主uId（即创建的人的User_Id和群组的GroupId）查找，获得GroupName
        String GroupName = g.getGroupName();  //获取群组Id为GroupId的群组名字GroupName
        //Group group = new Group(GroupId,GroupName);
        List<Group> GroupList = new ArrayList<>();
        for(Integer Id:Ids){
            Group group = new Group(Id,GroupId,GroupName);
            GroupList.add(group);
        }
        if(groupMapper.AddUserIntoGroup(GroupList)==GroupList.size()){
            return 1; //成功
        }else{
            return 0;
        }
    }



    /**
     * 修改群成员上传权限
     * @param GroupId
     * @param Ids
     * @param UpPermission
     * @return
     */
    @Override
    public Integer ModifyUpPermission(Integer GroupId,List<Integer> Ids,String UpPermission){
        return groupMapper.ModifyUpPermission(GroupId,Ids,UpPermission);
    }

    /**
     * 修改下载权限
     * @param GroupId
     * @param Ids
     * @param DlPermission
     * @return
     */
    @Override
    public Integer ModifyDlPermission(Integer GroupId,List<Integer> Ids,String DlPermission){
        return groupMapper.ModifyDlPermission(GroupId,Ids,DlPermission);
    }

    /**
     * 修改查看浏览权限
     * @param GroupId
     * @param Ids
     * @param LPermission
     * @return
     */
    @Override
    public Integer ModifyLPermission(Integer GroupId,List<Integer> Ids,String LPermission){
        return groupMapper.ModifyLPermission(GroupId,Ids,LPermission);
    }

    /**
     * 修改删除权限
     * @param GroupId
     * @param Ids
     * @param DPermission
     * @return
     */
    @Override
    public Integer ModifyDPermission(Integer GroupId,List<Integer> Ids,String DPermission){
        return groupMapper.ModifyDPermission(GroupId,Ids,DPermission);
    }

    /**
     * 修改重命名权限
     * @param GroupId
     * @param Ids
     * @param RePermission
     * @return
     */
    @Override
    public Integer ModifyRePermission(Integer GroupId,List<Integer> Ids,String RePermission){
        return groupMapper.ModifyRePermission(GroupId,Ids,RePermission);
    }

    /**
     * 修改移动权限
     * @param GroupId
     * @param Ids
     * @param MPermission
     * @return
     */
    @Override
    public Integer ModifyMPermission(Integer GroupId,List<Integer> Ids,String MPermission){
        return groupMapper.ModifyMPermission(GroupId,Ids,MPermission);
    }

    /**
     * 根据群组编号G_Id删除群组以及数据库相关信息
     * @param GroupId
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, readOnly = false)
    public Boolean DeleteGroup(Integer GroupId) {
        //1、获取该群组所有的文件id
        List<Integer> fileIds = fileBelongGroupMapper.SelectFileIdsByGroupId(GroupId);
        //2、先删除从表F_belong_G
        Integer deleteFBG = fileBelongGroupMapper.DeleteByGroupId(GroupId);
        if(deleteFBG == fileIds.size()){
            //3、成功删除从表后再删除主表group和file
            groupMapper.DeleteGByGroupId(GroupId);
            Integer deleteFile = fileMapper.deleteFileBatchs(fileIds);
            if(deleteFile == fileIds.size()){
                //4、删除objectSize表以及obs的群组文件夹
                Integer deleteObject = fileMapper.delete_ObjectSize("groups/" + GroupId.toString() + "/");
                Integer deleteObs = obsService.delete_Obsobject("groups/" + GroupId.toString() + "/");
                if(deleteObs == 1 && deleteObject == 1){
                    return true;
                }else {
                    return false;
                }
            }else {
                return false;
            }
        }else {
            return false;
        }
    }

    @Override
    public List<Integer> ShowUserBelongGroup(Integer U_Id) {
        return groupMapper.ShowUserBelongGroup(U_Id);
    }


}
